using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace EntLibContrib.Validation
{
    public class MultipleRuleSetValidator : Validator
    {
        string configurationFile;
        IEnumerable<string> ruleSets;

        public MultipleRuleSetValidator(string targetConfigurationFile, IEnumerable<string> targetRuleSets)
            :base(null, null)
        {
            this.configurationFile = targetConfigurationFile;
            this.ruleSets = targetRuleSets;
        }

        protected override string DefaultMessageTemplate
        {
            get { return null; }
        }

        protected override void DoValidate(object objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            if (objectToValidate != null)
            {
                Type targetType = objectToValidate.GetType();
                foreach (string ruleSet in ruleSets)
                {
                    Validator v = ValidationFactory.CreateValidator(targetType, ruleSet, new FileConfigurationSource(configurationFile));
                    v.Validate(objectToValidate, validationResults);
                }
            }
        }
    }
}
